<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>编辑页面</title>
</head>
<body>
<script type="text/html" id="form">
    <input type="hidden" name="id" value="{{= d.id }}" />
    <div class="mainBox">
        <div class="main-container">
            <div class="main-container">
				<div class="layui-form-item">
    				<label class="layui-form-label" title="名称"> <text title="必填项" style="color:red;">*</text> 名称</label>
    				<div class="layui-input-block">
            			<input autocomplete="off" type="text" name="name" id="name" placeholder="请输入名称" value="{{=d.name}}" class="layui-input" lay-verify="required">
    				</div>
				</div>

				<div class="layui-form-item">
    				<label class="layui-form-label" title="分类id"> <text title="必填项" style="color:red;">*</text> 分类</label>
    				<div class="layui-input-block">
            			<select name="category_id" id="category_id" lay-verify="required" placeholder="请选择分类id">
							<option value="">请选择分类</option>
							<option value="11" {{# if(d.category_id.toString() == '11'){ }}selected="selected"{{# } }}>11</option>
                        </select>
    				</div>
				</div>

				<div class="layui-form-item">
    				<label class="layui-form-label" title="状态"> <text title="必填项" style="color:red;">*</text> 状态</label>
    				<div class="layui-input-block">
            			<!-- 隐藏域设置开关未选中时需要传递的参数值 -->
                        <input type="hidden" name="status" id="status-0" value="0" />
                        <input type="checkbox" name="status" id="status-1" lay-skin="switch" lay-text="启用|禁用" value="1" {{# if(1 == d.status){ }}checked="checked"{{# } }} />
    				</div>
				</div>
            </div>
        </div>
    </div>
    <div class="bottom">
        <div class="button-container">
            <button type="submit" class="laytp-btn laytp-btn-primary laytp-btn-sm" lay-submit="" lay-filter="laytp-form">
                <i class="layui-icon layui-icon-ok"></i>
                提交
            </button>
            <button type="reset" class="laytp-btn laytp-btn-sm">
                <i class="layui-icon layui-icon-refresh"></i>
                重置
            </button>
        </div>
    </div>
</script>
<form class="layui-form" lay-filter="layui-form"></form>
<script>
    if(localStorage.getItem("staticDomain")){
        document.write("<link rel='stylesheet' href='" + localStorage.getItem("staticDomain") + "/component/laytp/css/laytp.css?v=" + localStorage.getItem("version") + "'>");
        document.write("<script src='" + localStorage.getItem("staticDomain") + "/component/layui/layui.js?v=" + Date.now() + "'><\/script>");
        document.write("<script src='" + localStorage.getItem("staticDomain") + "/component/laytp/layuiConfig.js?v=" + Date.now() + "'><\/script>");
    }else{
        document.write("<link rel='stylesheet' href='/static/component/laytp/css/laytp.css?v=" + localStorage.getItem("version") + "'>");
        document.write("<script src='/static/component/layui/layui.js?v=" + Date.now() + "'><\/script>");
        document.write("<script src='/static/component/laytp/layuiConfig.js?v=" + Date.now() + "'><\/script>");
    }
</script>
<script>
    layui.use(['laytp'],function(){
        let form = layui.form;
        let $ = layui.jquery;
        //获取参数ID
        var id = facade.getUrlParam('id');
        if(!id){
            facade.error('参数ID不能为空');
            return false;
        }



        //获取数据，渲染到对应的节点上
        facade.ajax({
            route: "/admin.course/info",
            data: {id: id},
            successAlert: false,
            showLoading: true
        }).done(function(res){
            if(res.code === 0){
                d = res.data; // 确保 d.category_id 和 d.course_id 在模板渲染前已赋值

                facade.ajax({
                    route: "/admin.category/index",
                    successAlert: false,
                    showLoading: true
                }).done(function (res) {
                    if (res.code === 0) {
                        let categorySelect = $('#category_id');
                        categorySelect.empty(); // 清空原有选项
                        categorySelect.append('<option value="">请选择分类</option>'); // 添加默认选项

                        // 遍历接口返回的分类数据，动态生成选项
                        res.data.forEach(function (category) {
                            let selected = d.category_id && category.id.toString() === d.category_id.toString() ? 'selected="selected"' : '';
                            categorySelect.append(`<option value="${category.id}" ${selected}>${category.name}</option>`);
                        });

                        form.render('select'); // 刷新表单组件
                    }
                });


                layui.laytpl($("#form").html()).render(res.data,function(string){
                    $("form").html(string);
                    layui.laytpForm.render();
                    form.render();
                });
            }
        });

        form.on('submit(laytp-form)', function(data){
            var btnAnim = layui.button.load({elem:$(this)});
            try{
                data = facade.setEditorField(data);
            }catch (e) {
                facade.error(e);
                return false;
            }
            facade.ajax({
                route:'/admin.course/edit',
                data:data.field
            }).done(function(res){
                if(res.code === 0){
                    parent.layui.layer.close(parent.layui.layer.getFrameIndex(window.name));//关闭当前页
                    parent.layui.table.reloadData("laytp-table");
                }
                btnAnim.stop();
            }).fail(function(){
                btnAnim.stop();
            });
            return false;
        });
    })
</script>
</body>
</html>